﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace SHomeWorkshop.LunarPresentation.Expressions
{
    /// <summary>
    /// “MS”是指Master SubPanel/Slave SubPanel。
    /// 这个类是一个中间类，适用于嵌入两个子面板的情况。
    /// </summary>
    public abstract class MSExpression : StackPanelExpression
    {
        public MSExpression(SubPanel rootPanel, SubPanel parentPanel, Editor editor)
            : base(rootPanel, parentPanel, editor)
        {
            this.subPanels = new List<SubPanel>();
            InitializeMSPanels(rootPanel, editor);
            RefreshSubPanelRelatios();
        }

        /// <summary>
        /// 子类应根据需要覆盖本方法，例如：Bracket就需要在两个面板之间添加一个
        /// “BracketPanel”来绘制一个括弧。
        /// </summary>
        /// <param name="rootPanel">根面板。</param>
        /// <param name="editor">编辑器。</param>
        protected virtual void InitializeMSPanels(SubPanel rootPanel, Editor editor)
        {
            this.basePanel.Orientation = Orientation.Horizontal;

            this.masterPanel = new SubPanel(rootPanel, this.ParentPanel, editor, this);
            this.masterPanel.BasePanel.VerticalAlignment = VerticalAlignment.Center;
            this.basePanel.Children.Add(this.masterPanel);

            this.slavePanel = new SubPanel(rootPanel, this.ParentPanel, editor, this);
            this.slavePanel.BasePanel.VerticalAlignment = VerticalAlignment.Center;
            this.basePanel.Children.Add(this.slavePanel);

            RefreshSubPanelRelatios();
        }

        protected SubPanel masterPanel;

        protected SubPanel MasterPanel
        {
            get { return masterPanel; }
        }

        protected SubPanel slavePanel;

        public SubPanel SlavePanel
        {
            get { return slavePanel; }
        }

        /// <summary>
        /// SubPanels属性用以在公式各层间导航，不是所有公式内部都嵌入子面板，
        /// 因此不是所有公式均需要本方法。
        /// 
        /// 在实际公式类，此一方法应设置：FirstSubPanel/LastSubPanel/SubPanels
        /// 这三个属性的值。
        /// </summary>
        protected abstract void RefreshSubPanelRelatios();

        public abstract void TurnOffMSPanel();

        protected abstract void RunTurnOff();

        public override void SetProperty(string propertyName, string value)
        {
            if (propertyName == null || propertyName.Length <= 0) return;
            base.SetProperty(propertyName, value);

            switch (propertyName)
            {
                case "TurnOffMSPanel":
                    {
                        RunTurnOff();//翻转比较简单，直接再执行一次即可。
                        break;
                    }
            }
        }
    }
}
